/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.util.math;

/**
 * @author chunquedong
 * 
 */
public class MyDouble {

	private static double threshold = 1e-10;

	/**
	 * 设置阈值，负数表示使用系统默认值
	 * 
	 * @param value
	 */
	public static void setThreshold(double value) {
		threshold = value;
	}

	public static boolean approximateEquals(double a, double b) {
		return approximateEquals(a, b, threshold);
	}

	public static boolean approximateEquals(double a, double b,
			double distinguish) {
		double bZ = Math.abs(a - b);
		if (bZ > distinguish)
			return false;
		return true;
	}

	public static int approximateCompare(double a, double b) {
		return approximateCompare(a, b, threshold);
	}

	public static int approximateCompare(double a, double b, double distinguish) {
		double bZ = a - b;
		if (bZ > 0) {
			if (bZ > distinguish)
				return 1;
			else
				return 0;
		} else {
			bZ = -bZ;
			if (bZ > distinguish)
				return -1;
			else
				return 0;
		}
	}

	public static boolean isNegativeInfinity(double d) {
		String s = d + "";
		return s.equals("-Infinity");
	}

	public static boolean isPositiveInfinity(double d) {
		String s = d + "";
		return s.equals("Infinity");
	}
}
